﻿using Entity;
using IService;
using Microsoft.AspNetCore.Mvc;
using Service;

namespace XingWeiDaWMSProject.Controllers
{
    public class OutboundManagementController : Controller
    {
        private readonly IInventoryService _inventoryService;
        private readonly IOutboundManagementService _outboundManagementService;

        public OutboundManagementController(IInventoryService inventoryService,IOutboundManagementService outboundManagementService)
        {
            _inventoryService = inventoryService;
            _outboundManagementService = outboundManagementService;
        }
        public IActionResult Index()
        {
            if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                // AJAX请求：返回部分视图（无布局）
                return PartialView();
            }
            else
            {
                // 普通请求：返回完整视图（带布局）
                return View();
            }
        }


        public IActionResult Add()
        {
            if (Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                // AJAX请求：返回部分视图（无布局）
                return PartialView();
            }
            else
            {
                // 普通请求：返回完整视图（带布局）
                return View();
            }
        }

        // 仓库列表查询接口（供前端调用）
        [HttpGet] // 接口路径：api/Warehouse/GetAllWarehouses
        public async Task<IActionResult> GetAllWarehouses()
        {
            try
            {
                // 调用你已写好的 GetAllAsync 方法，获取未删除的仓库列表
                List<WarehouseEntity> warehouses = await _inventoryService.GetAllWarehousesAsync();

                // 返回成功结果（前端可直接解析数据）
                return Ok(new
                {
                    success = true,
                    data = warehouses
                });
            }
            catch (Exception ex)
            {
                // 异常处理，返回错误信息
                return Ok(new
                {
                    success = false,
                    message = "查询仓库失败：" + ex.Message
                });
            }
        }


        //出库管理新增关联单号分页查询功能
        [HttpGet]
        public async Task<IActionResult> GetPagedOutbound(
           string sales_order_no = null,
           string document_status = null,
           string customer_name = null,
           DateTime? sales_date = null,
            string sales_person = null,
            string sales_dept = null,
           int currentPage = 1,
           int pageSize = 2)
        {
            try
            {
                var result = await _outboundManagementService.GetAllWMSOutboundAsync(
                    sales_order_no, document_status, customer_name, sales_date, sales_person, sales_dept, currentPage, pageSize);
                // 返回规范格式（前端可直接解析）
                return Json(new
                {
                    success = true,
                    data = result.items,  // 当前页数据
                    total = result.total, // 总条数
                    message = "查询成功"
                });

            }
            catch (Exception ex)
            {
                // 输出异常详情到日志（建议结合日志框架如Serilog/NLog）
                Console.WriteLine($"查询出库管理分页数据异常：{ex.Message}\n{ex.StackTrace}");
                // 返回友好的错误提示
                return StatusCode(500, new { Message = "服务器处理请求时发生异常，请联系管理员", ErrorDetail = ex.Message });
            }
        }


        /// <summary>
        /// 根据销售单号查询货品明细
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetBySalesOrderNo(string salesOrderNo)
        {
      
            // 2. 查询货品明细
            var details = await _outboundManagementService.GetOutboundById(salesOrderNo);
            return Ok(details);
        }



    }
}
